<template>
  <div class="app-container">

    <el-table v-loading="loading" :data="praTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="企业" align="center" prop="company.title" />
      <el-table-column label="岗位" align="center" prop="post" />
      <el-table-column label="学期" align="center" prop="semster.title" />
      <el-table-column label="开始日期" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="实习成绩" align="center" prop="score" />
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == '3'"
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleAdd(scope.row)"
          >总结</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleTask(scope.row)"
          >详细</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleCheck(scope.row)"
          >考勤</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改实习总结对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="内容" prop="content">
          <el-input rows="3" v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="附件" prop="fileId">
          <span v-html="getDownLink(form)" v-if="form.file != null"></span>
          <el-upload
            ref="upload"
            :limit="1"
            accept=".doc, .docx, .pdf, .jpg, .jpeg, .png"
            :headers="upload.headers"
            :action="upload.url + '?updateSupport=' + upload.updateSupport"
            :disabled="upload.isUploading"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许上传文档或者图片文件！</div>
        </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查看考勤对话框 -->
    <el-dialog  :close-on-click-modal="false" title="考勤记录" :visible.sync="checkShow" width="720px" append-to-body>
    <el-table v-loading="loading" :data="check.list">
      <el-table-column label="考勤日期" align="center" prop="checkDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考勤类型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="上岗时间" align="center" prop="onTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.onTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离岗时间" align="center" prop="offTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.offTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="check.total>0"
      :total="check.total"
      :page.sync="check.pageNum"
      :limit.sync="check.pageSize"
      @pagination="getList"
    />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelCheck">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 实习任务对话框 -->
    <el-dialog  :close-on-click-modal="false" title="实习任务" :visible.sync="taskShow" width="500px" append-to-body>
      <el-form ref="task" :model="task" disabled label-width="80px">
        <el-form-item label="岗位" prop="post">
          <el-input v-model="task.post" placeholder="请输入岗位" />
        </el-form-item>
        <el-form-item label="开始日期" prop="startDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="task.startDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="task.endDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实习内容" prop="content">
          <el-input
            v-model="task.content"
            placeholder="实习内容"
            clearable
            type="textarea"
            :rows="2"
            size="small"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="task.status" placeholder="请选择状态">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelTask">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPraSummary, getPraSummary, delPraSummary, addPraSummary, updatePraSummary, exportPraSummary } from "@/api/practice/praSummary";
import { myPraTask, getPraTask } from "@/api/practice/praTask";
import { listPraCheck } from "@/api/practice/praCheck";
import { selectCompany} from "@/api/base/company";
import { getToken } from "@/utils/auth";

export default {
  name: "MyPraSummary",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实习总结表格数据
      praTaskList: [],
      // 考勤是否提交
      booleanOptions: [],
      // 状态字典
      statusOptions: [],
      // 考勤类型字典
      typeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/sys/files/upload"
      },
      check: {
        pageNum: 1,
        pageSize: 10,
        show:false,
        list: []
      },
      checkShow: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuId: null,
        companyId: null,
        content: null,
        addTime: null,
        fileId: null
      },
      task: {},
      taskShow: false,
      // 表单参数
      form: {},
      // 日期范围
      dateRange: [],
      stuId: null,
      // 表单校验
      rules: {
        stuId: [
          { required: true, message: "学生不能为空", trigger: "blur" }
        ],
        companyId: [
          { required: true, message: "企业不能为空", trigger: "blur" }
        ],
        addTime: [
          { required: true, message: "提交时间不能为空", trigger: "blur" }
        ],
        fileId: [
          { required: true, message: "附件不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("practice_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("practice_check").then(response => {
      this.typeOptions = response.data;
    });
  },
  methods: {
    /** 查询实习总结列表 */
    getList() {
      this.loading = true;
      myPraTask().then(response => {
        this.praTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
        if(response.total > 0){
          this.stuId = response.rows[0]['stuId'];
        }
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 考勤类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name:null,
        year:null,
        classId: null,
        specId: null,
        companyId: null,
        semId: null,
        stuId: null,
        companyId: null,
        content: null,
        addTime: null,
        file: null,
        fileId: null
      };
      this.resetForm("form");
    },
    /** 下载附件链接 */
    getDownLink(row) {
      if(row.file && row.file.id){
        return '<a href="'+process.env.VUE_APP_BASE_API+'/sys/files/download/'+row.file.id+'" target="_blank">'+row.file.title+'</a>';
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      listPraSummary({semId:row.semId,stuId:row.stuId,companyId:row.companyId}).then(response => {
        if(response.total > 0){
          this.form = response.rows[0];
          this.open = true;
          this.title = "修改实习总结";
        }else{
          this.reset();
          this.form.stuId = row.stuId;
          this.form.companyId = row.companyId;
          this.open = true;
          this.title = "添加实习总结";
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePraSummary(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPraSummary(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 详细操作 */
    handleCheck(row) {
      listPraCheck({stuId:row.stuId, companyId:row.companyId}).then(response => {
        this.check.list = response.rows;
        this.check.total = response.total;
        this.checkShow = true;
      });
    },
    // 取消按钮
    cancelCheck() {
      this.checkShow = false;
    },

    /** 详细操作 */
    handleTask(row) {
      const id = row.id || this.ids
      getPraTask(id).then(response => {
        this.task = response.data;
        this.taskShow = true;
      });
    },
    // 取消按钮
    cancelTask() {
      this.taskShow = false;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有实习总结数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPraSummary(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.form.fileId = response.data.id
      this.form.file = response.data
      this.$refs.upload.clearFiles();
    },
  }
};
</script>
